import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

public class GeneratorSqlmap {

	private File getResourceFile(Class clazz,String resourceFileName){
		URL url = clazz.getResource(resourceFileName);
		String encode = url.getFile();
		String path = null;
		try{
			path = URLDecoder.decode(encode, "UTF-8");
			File file = new File(path);
			if (file.exists()){
				return file;
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return null;
	}

	public void generator() throws Exception{
		List<String> warnings = new ArrayList<>();
		boolean overwrite = true;

		File configFile = getResourceFile(GeneratorSqlmap.class,"generatorConfig.xml");
		System.out.println(configFile.getAbsolutePath());
		ConfigurationParser cp = new ConfigurationParser(warnings);
		Configuration config = cp.parseConfiguration(configFile);
		DefaultShellCallback callback = new DefaultShellCallback(overwrite);
		MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
				callback, warnings);
		myBatisGenerator.generate(null);//自动生成
	}

}
